package com.hui.auth.dao;

import com.hui.auth.dao.sql.CommonSQLProvider;
import com.hui.auth.entity.AuthUser;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface AuthUserDao {

    /**
     * 插入用户
     */
    @InsertProvider(type = CommonSQLProvider.class, method = "insert")//哪个类的哪个方法
    @Options(useGeneratedKeys = true)
    boolean insert(AuthUser user);

    /**
     * 关联角色
     */
    boolean insertUserRole(@Param("userId") Long userId, @Param("roleIds") List<Integer> roleIds);

    /**
     * 改
     */
    @UpdateProvider(type = CommonSQLProvider.class, method = "update")
    Integer update(AuthUser user);

    /**
     * 查询所有用户
     */
    @Select("SELECT * FROM auth_user WHERE user_name like CONCAT('%',#{search},'%')")
    List<AuthUser> selectAll(Map<String, Object> params);

    /**
     * 删除
     */
    @Delete("DELETE FROM `auth_user` WHERE id = #{id}")
    Integer delete(Long id);

    /**
     * 删除用户相关角色
     */
    @Delete("DELETE FROM `auth_user_role` WHERE uid = #{uid}")
    Integer deleteRoleByUid(Long uid);

    /**
     * 查询用户
     *
     * @apiNote 此处数据库用户名字段要用：utf8mb4_bin
     */
    @Select("SELECT * FROM auth_user WHERE user_name =#{username}")
    AuthUser selectByUserName(String username);

}
